package io.github.wjehovah.yyn.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;

import java.util.List;

import io.github.wjehovah.yyn.R;
import io.github.wjehovah.yyn.bean.SubscriptBean.SubscriptDetail;

/**
 * 搜索结果页面的Adapter
 * Created by yingchen on 2015/8/12.
 */
public class SubscriptSearchAdapter extends BaseAdapter {
    private Context context;
    private List<SubscriptDetail> data;
    private BitmapUtils mBitmapUtils;
    public SubscriptSearchAdapter( Context context,List<SubscriptDetail> data){
        this.context = context;
        this.data = data;
        mBitmapUtils = new BitmapUtils(context);
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();

            convertView = View.inflate(context, R.layout.subscript_item,null);

            holder.ivSubscriptIcon = (ImageView) convertView.findViewById(R.id.iv_item_subscript_icon);
            holder.tvSubscripyName = (TextView) convertView.findViewById(R.id.tv_item_subscript_name);
            holder.tvSubscriptCount = (TextView) convertView.findViewById(R.id.tv_item_subscript_count);

            holder.btnSubscript = (Button) convertView.findViewById(R.id.btn_item_subscript);

            convertView.setTag(holder);
        }else{
            holder = (ViewHolder) convertView.getTag();
        }

        //获取对应位置的数据
        SubscriptDetail subscriptDetail = data.get(position);

        //设置头像
        //判断头像连接是否为空
        if(TextUtils.isEmpty(subscriptDetail.image_list)){ //为空
            holder.ivSubscriptIcon.setImageResource(R.drawable.label_default_icon);
        }else{
            mBitmapUtils.display(holder.ivSubscriptIcon,subscriptDetail.image_list);
        }

        //设置名称
        holder.tvSubscripyName.setText(subscriptDetail.theme_name);
        holder.tvSubscriptCount.setText(subscriptDetail.sub_number+"人订阅");

        //隐藏按钮
        holder.btnSubscript.setVisibility(View.GONE);
        return convertView;
    }

    static  class ViewHolder{
        public ImageView ivSubscriptIcon;
        public TextView tvSubscripyName;
        public TextView tvSubscriptCount;
        public Button btnSubscript;
    }

}
